<?php include("includes/session.php"); ?>
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title><?php echo $lang['PAGE_TITLE_MESSAGES']; ?></title>
<?php include("header.php"); ?>
<?php
if(!$session->logged_in)
{
?>
	<br />
	<center>
	<table cellpadding="0" cellspacing="0" width="70%">
	<tr>
		<td class="topLeft"></td><td bgcolor="#dadada"></td><td class="topRight"></td>
	</tr>
	<tr bgcolor="#dadada">
	<td></td><td>
		<center><b>You Are Not Logged In</b></center><br />
		<center>Please <a href="index.php">Log In</a> to View This Page, or <a href="register.php">Register</a> Now. </center><br />
		<center>Thank You.</center>
	</td><td></td>
	</tr>
	<tr>
		<td class="bottomLeft"></td><td bgcolor="#dadada"></td><td class="bottomRight"></td>
	</tr>
	</table>
	</center>
<?php
}
else
{
?>
<table cellpadding="20" cellspacing="0" width="100%">
<tr><td>
	<?php
	if(isset($_GET['read']))
	{
		$_GET['read'] = htmlspecialchars($_GET['read']);
	?>
	<center>
	<table cellpadding="0" cellspacing="0">
	<tr>
		<td class="topLeft"></td><td bgcolor="#dadada"></td><td class="topRight"></td>
	</tr>
	<tr bgcolor="#dadada">
	<td></td><td>
	<center><font size="+1"><b><?php echo $lang['MESSAGES_MESSAGE']; ?></b></font></center><br />
	<?php
	$retval = $database->query("SELECT * FROM ".TBL_MESSAGES." WHERE id='".$_GET['read']."'");
	$message = mysql_fetch_array($retval);
	$result = $database->query("SELECT profilePic FROM ".TBL_USERS." WHERE username='".$message['fromUser']."'");
	$from = mysql_fetch_array($result);
	
	if(!$retval || mysql_num_rows($retval)<1 || !$result || mysql_num_rows($result)<1)
	{
		echo "<font size=\"2\" color=\"#ff0000\">No Message Found</font>";
	}
	else
	{
		if($message['viewed']==0)
			$database->markRead($_GET['read']);
	?>
	<table cellpadding="5" cellspacing="0">
	<tr>
		<td valign="top"><img width="100" height="100" border="1" src="<?php if($from['profilePic']!=''){ echo $from['profilePic']; }else{ echo DEFAULT_PROFILE_PIC; } ?>" /></td>
		<td>
		<table cellpadding="5" cellspacing="0">
		<tr>
			<td align="right"><b><?php echo $lang['MESSAGES_FROM']; ?></b></td>
			<td align="left"><?php echo "<a href=\"profile.php?user=".$message['fromUser']."\" title=\"View Profile\">".$message['fromUser']."</a>"; ?></td>
		</tr>
		<tr>
			<td align="right"><b><?php echo $lang['MESSAGES_SUBJECT']; ?></b></td>
			<td align="left"><?php echo $message['subject']; ?></td>
		</tr>
		<tr>
			<td align="right" valign="top"><b><?php echo $lang['MESSAGES_THEMESS']; ?></b></td>
			<td align="left"><?php echo $message['message']; ?></td>
		</tr>
		<tr>
			<td align="right" valign="top"></td>
			<td align="left"><a href="messages.php?send=<?php echo $message['fromUser']; ?>&reply=<?php echo $_GET['read']; ?>" title="Reply to Message">Reply</a></td>
		</tr>
		</table>
		</td>
	</tr>
	</table>
	<?php
		}
	?>
	</td><td></td>
	</tr>
	<tr>
		<td class="bottomLeft"></td><td bgcolor="#dadada"></td><td class="bottomRight"></td>
	</tr>
	</table>
	</center>
	<br />
	<?php
	}
	else if(isset($_GET['send']))
	{
		$_GET['send'] = htmlspecialchars($_GET['send']);
		$result = $database->query("SELECT profilePic FROM ".TBL_USERS." WHERE username='".$_GET['send']."'");
		$to = mysql_fetch_array($result);
	?>
	<center>
	<table cellpadding="0" cellspacing="0">
	<tr>
		<td class="topLeft"></td><td bgcolor="#dadada"></td><td class="topRight"></td>
	</tr>
	<tr bgcolor="#dadada">
	<td></td><td>
	<center><font size="+1"><b><?php echo $lang['MESSAGES_SEND']; ?></b></font></center><br />
	<center>
	<?php
		if(isset($_SESSION['sendMessageSuccess']) && $_SESSION['sendMessageSuccess'])
		{
			echo "<font size=\"2\" color=\"#00c809\">Message Sent Successfully</font>";
			unset($_SESSION['sendMessageSuccess']);
		}
		else
		{
			if(!$_SESSION['sendMessageSuccess'])
			{
				echo "<font size=\"2\" color=\"#ff0000\">Message Failed<br />Please Try Again Later</font>";
				unset($_SESSION['sendMessageSuccess']);
			}
			if(isset($_GET['reply']))
			{
				$result = $database->query("SELECT * FROM ".TBL_MESSAGES." WHERE id='".$_GET['reply']."'");
				$message = mysql_fetch_array($result);
			}
	?>
	<table cellpadding="5" cellspacing="0">
	<tr>
		<td valign="top">
			<?php echo "<center><b>".$session->username."</b></center>"; ?>
			<center><img width="100" height="100" border="1" src="<?php if($session->userinfo['profilePic']!=''){ echo $session->userinfo['profilePic']; }else{ echo DEFAULT_PROFILE_PIC; } ?>" /></center>
		</td>
		<td>
			<form action="includes/process.php" method="POST">
			<input type="hidden" name="subSendMessage" value="<?php echo $_GET['send']; ?>"/>
			<table cellpadding="2" cellspacing="0">
			<tr>
				<td align="right"><b><?php echo $lang['MESSAGES_SUBJECT']; ?></b></td>
				<td align="left"><input type="text" name="subject" size="54" value="<?php if($form->value("subject")!=""){ echo $form->value("subject"); }else if(isset($_GET['reply'])){ echo "RE:".$message['subject']; } ?>" /></td>
				<td align="left"><?php echo $form->error("subject"); ?></td>
			</tr>
			<tr>
				<td align="right"><b><?php echo $lang['MESSAGES_THEMESS']; ?></b></td>
				<td align="left"><textarea name="message" cols="41" rows="4"><?php echo $form->value("message"); ?></textarea></td>
				<td align="left"><?php echo $form->error("message"); ?></td>
			</tr>
			<tr>
				<td></td>
				<td align="left"><input type="submit" value="Send Message" /></td>
			</tr>
			</table>
			</form>
		</td>
		<td valign="top">
			<?php echo "<center><b>".$_GET['send']."</b></center>"; ?>
			<img width="100" height="100" border="1" src="<?php if($to['profilePic']!=''){ echo $to['profilePic']; }else{ echo DEFAULT_PROFILE_PIC; } ?>" />
		</td>
	</tr>
	</table>
	<?php
			if(isset($_SESSION['sendMessageSuccess']))
				unset($_SESSION['sendMessageSuccess']);
		}
	?>
	</center>
	</td><td></td>
	</tr>
	<tr>
		<td class="bottomLeft"></td><td bgcolor="#dadada"></td><td class="bottomRight"></td>
	</tr>
	</table>
	</center>
	<br />
	<?php
	}
	?>
	<center>
	<table cellpadding="0" cellspacing="0" width="100%">
	<tr>
		<td class="topLeft"></td><td bgcolor="#dadada"></td><td class="topRight"></td>
	</tr>
	<tr bgcolor="#dadada">
	<td></td><td>
	<center><font size="+1"><b><?php echo $lang['MESSAGES_MESSAGE']; ?></b></font></center><br />
		<?php displayMessages($session->username); ?>
	</td><td></td>
	</tr>
	<tr>
		<td class="bottomLeft"></td><td bgcolor="#dadada"></td><td class="bottomRight"></td>
	</tr>
	</table>
	</center>
</td></tr>
</table>
<?php } ?>
<?php include("footer.php"); ?>